How orderBy multiple fields in Angular?
How orderBy multiple fields in Angular?
482
17-Jun-2024
Updated on 19-Jun-2024
Ashutosh Kumar Verma
19-Jun-2024Order By Multiple Fileds
In AngularJS, you can use the
orderByfilter to sort objects based on different fields. Here's how you canorderBymultiple fields:Syntax
The orderBy filter accepts a set of predicate functions or strings, where each predicate can refer to a field and optionally specifies a sort direction (
+for ascending,-for descending).In the example above
itemsare the array of objects you want to display.orderBy:['field1', 'field2']Specifies that the item order should be sorted first in ascending order by field1, then in ascending order by field2 by default.Array data
Let's take an array of objects in the controller,
And you want to show these factors first in ascending order by
city, then in descending order byage. You can use orderByIn the syntax above-
orderBy:['city', '-age']First sort theitemsin ascending order by city ('city'), then in descending order by age ('-age').the
cityis arranged in order of ascending order.-agesorts age in descending order (using-before the field name in descending order).Example-
Here's a basic example that demonstrates the multiple orderBy properties in Angular js
Output-

Also, Read: How to use a keypress event in AngularJS?